﻿Imports util
Imports System.Data.OleDb

Public Class OdeiService
    Public Function findAll() As List(Of Odei)
        Dim lista As New List(Of Odei)

        Dim con = contexto.conBD
        Try
            Dim cmd As New OleDbCommand()
            cmd.Connection = con
            con.Open()

            Dim sql As String = "select * from Odei"
            cmd.CommandText = sql
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            While dr.Read
                Dim odei As New Odei
                odei.codigo = dr("cododei").ToString()
                odei.nombre = dr("descripcion").ToString()
                lista.Add(odei)
            End While

        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            con.Close()
        End Try

        Return lista
    End Function

    Public Function findByDepartamento(ByVal codigo As String) As List(Of Odei)
        Dim lista As New List(Of Odei)

        Dim con = contexto.conBD
        Try
            Dim cmd As New OleDbCommand()
            cmd.Connection = con
            con.Open()

            If codigo = "07" Then 'Callao
                codigo = "15" 'para que busque Odei Lima
            End If

            Dim sql As String = "select * from Odei where ccdd='" & codigo & "'"
            cmd.CommandText = sql
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            While dr.Read
                Dim odei As New Odei
                odei.codigo = dr("cododei").ToString()
                odei.nombre = dr("descripcion").ToString()
                lista.Add(odei)
            End While

        Catch ex As Exception
            MsgBox(ex.ToString)
        Finally
            con.Close()
        End Try

        Return lista
    End Function
End Class
